package com.hjy.web;

import com.hjy.utils.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class GeneratorController {

    @Autowired
    private DataUtil dataUtil;

    // 生成代码
    @RequestMapping("code")
    public void generateCode(HttpServletResponse response) throws IOException {

        byte[] bytes = dataUtil.generatorCodes();

        // 6. 设置响应头
        response.reset(); // 清除缓冲区中存在的所有数据以及状态代码和标题
        response.setHeader("Content-Disposition", "attachment; filename=\"miemie.zip\"");
        response.addHeader("Content-Length", "" + bytes.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        // 5. 返回前端
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
    }

    // 生成代码
    @RequestMapping("/")
    public String gotoIndex()  {
        return "/index.html";
    }
}
